﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using ForumLibrary.Data;
using System.Collections;
using System.Reflection;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }


    [WebMethod]
    public string AddPost(string username, string forumName, string threadName, string header, string body)
    {
        ForumManager forumManager = new ForumWebManager();
        return forumManager.addPost(username, forumName, threadName, header, body);
    }
    [WebMethod]
    public string EditPost(string username, string forumName, string threadName,string oldHeader, string header, string body)
    {
        ForumManager forumManager = new ForumWebManager();
        return forumManager.editPost(username, forumName, threadName,oldHeader, header, body);
    }
    [WebMethod]
    public List<SPost> getSPosts(string forumName, string threadName)
    {
        ForumManager manager = new ForumWebManager();
        List<Post> posts = (List<Post>)manager.getPosts(forumName, threadName);
        List<SPost> sPosts = new List<SPost>();
        foreach (Post p in posts)
        {
            sPosts.Add(new SPost(p.Thread, p.Forum, p.Header, p.Body, String.Format("{0:dd/MM/yyyy HH:mm tt}",p.PublishDate), p.Creator));
        }
        return sPosts;
    }

    [WebMethod]
    public List<SThread> getSThreads(string forumName)
    {
        ForumManager manager = new ForumWebManager();
        List<Thread> threads = (List<Thread>)manager.getThreads(forumName);
        List<SThread> sThreads = new List<SThread>();
        foreach (Thread t in threads)
        {
            sThreads.Add(new SThread(t.Forum, t.Header, t.Body, t.NumOfReplies, t.NumOfViews, t.Creator, String.Format("{0:dd/MM/yyyy HH:mm tt}", t.PublishDate), String.Format("{0:dd/MM/yyyy HH:mm tt}", t.LastDate)));
        }
        return sThreads;
    }

    [WebMethod]
    public string AddThread(string username, string forumName, string header, string body)
    {
        ForumManager manager = new ForumWebManager();
        return manager.addThread(username, forumName, header, body);
    }

    [WebMethod]
    public string DeleteThread(string username, string forumName, string header)
    {
        ForumManager manager = new ForumWebManager();
        return manager.deleteThread(username, forumName, header);
    }
    [WebMethod]
    public string EditThread(string username, string forumName, string oldHeader, string header, string body)
    {
        ForumManager manager = new ForumWebManager();
        return manager.editThread(username, forumName, oldHeader, header, body);
    }

    [WebMethod]
    public string DeletePost(string username, string forumName, string thread, string post)
    {
        ForumManager manager = new ForumWebManager();
        return manager.deletePost(username, forumName, thread, post);
    }

    [WebMethod]
    public bool dataChanged(string username, string pageType)
    {
        ForumManager mngr = new ForumWebManager();
        if (username != null)
        {
            bool flagChanged = mngr.getChangedFlag(pageType, username);
            return flagChanged;
        }
        return false;
    }

    [WebMethod]
    public int checkForNewNotifications(string username)
    {
        ForumManager mngr = new ForumWebManager();
        return mngr.lastNotificationsNum(username);
    }
}
